package LeetCode.month03.day81;

import java.util.TreeMap;

/**
 * @author 16778
 * @date 2025/10/21 8:52
 * @description
 */
public class No1094 {
    public boolean carPooling(int[][] trips, int capacity) {
        int n = trips.length;
        int[] arr = new int[1000];
        for (int i = 0; i < n; i++) {
            for (int j = trips[i][1]; j < trips[i][2]; j++) {
                arr[j]+=trips[i][0];
                if(arr[j]>capacity) return false;
            }
        }
        return true;
    }

    public boolean carPooling2(int[][] trips,int capacity){
        TreeMap<Integer,Integer> map = new TreeMap<>();
        for (int[] trip : trips) {
            map.merge(trip[1],trip[0],Integer::sum);
            map.merge(trip[2],-trip[0],Integer::sum);
        }
        int sum = 0;
        for (Integer value : map.values()) {
            sum+=value;
            if(sum>capacity) return false;
        }
        return true;
    }
}
